import { ref, provide, inject } from 'vue'
import { ElMessage } from 'element-plus'

// 创建一个唯一的key
const teacherStoreKey = Symbol('teacherStore')

// 初始讲师数据
const initialTeachers = [
  {
    id: 1,
    name: '张老师',
    title: '高级讲师',
    specialty: 'Vue.js, React, 前端工程化',
    avatar: 'https://picsum.photos/200/200?random=1',
    introduction: '10年前端开发经验，精通Vue.js和React框架，擅长前端工程化和性能优化。',
    tags: ['前端开发', 'Vue.js', 'React'],
    courseCount: 5,
    studentCount: 1234,
    rating: 4.9
  },
  {
    id: 2,
    name: '李老师',
    title: '资深讲师',
    specialty: 'Node.js, Python, 后端架构',
    avatar: 'https://picsum.photos/200/200?random=2',
    introduction: '8年后端开发经验，精通Node.js和Python，擅长系统架构设计和高性能服务开发。',
    tags: ['后端开发', 'Node.js', 'Python'],
    courseCount: 4,
    studentCount: 2345,
    rating: 4.8
  },
  {
    id: 3,
    name: '王老师',
    title: '技术专家',
    specialty: '人工智能, 机器学习, 深度学习',
    avatar: 'https://picsum.photos/200/200?random=3',
    introduction: '12年AI领域经验，精通机器学习和深度学习，多个大型AI项目负责人。',
    tags: ['人工智能', '机器学习', '深度学习'],
    courseCount: 3,
    studentCount: 3456,
    rating: 4.7
  },
  {
    id: 4,
    name: '赵老师',
    title: '高级讲师',
    specialty: 'Java, Spring Boot, 微服务',
    avatar: 'https://picsum.photos/200/200?random=4',
    introduction: '9年Java开发经验，精通Spring Boot和微服务架构，擅长高并发系统设计。',
    tags: ['Java', 'Spring Boot', '微服务'],
    courseCount: 4,
    studentCount: 4567,
    rating: 4.6
  },
  {
    id: 5,
    name: '钱老师',
    title: '资深讲师',
    specialty: '移动开发, Flutter, React Native',
    avatar: 'https://picsum.photos/200/200?random=5',
    introduction: '7年移动开发经验，精通Flutter和React Native，擅长跨平台应用开发。',
    tags: ['移动开发', 'Flutter', 'React Native'],
    courseCount: 3,
    studentCount: 5678,
    rating: 4.5
  },
  {
    id: 6,
    name: '孙老师',
    title: '技术专家',
    specialty: '数据库, MySQL, MongoDB',
    avatar: 'https://picsum.photos/200/200?random=6',
    introduction: '10年数据库开发经验，精通MySQL和MongoDB，擅长数据库优化和架构设计。',
    tags: ['数据库', 'MySQL', 'MongoDB'],
    courseCount: 4,
    studentCount: 6789,
    rating: 4.4
  },
  {
    id: 7,
    name: '周老师',
    title: '高级讲师',
    specialty: 'DevOps, Docker, Kubernetes',
    avatar: 'https://picsum.photos/200/200?random=7',
    introduction: '8年运维开发经验，精通Docker和Kubernetes，擅长容器化部署和自动化运维。',
    tags: ['DevOps', 'Docker', 'Kubernetes'],
    courseCount: 3,
    studentCount: 7890,
    rating: 4.3
  },
  {
    id: 8,
    name: '吴老师',
    title: '资深讲师',
    specialty: '数据分析, Python, 机器学习',
    avatar: 'https://picsum.photos/200/200?random=8',
    introduction: '6年数据分析经验，精通Python和机器学习，擅长数据挖掘和可视化。',
    tags: ['数据分析', 'Python', '机器学习'],
    courseCount: 4,
    studentCount: 8901,
    rating: 4.2
  }
]

// 创建讲师状态管理
export function createTeacherStore() {
  const teacherList = ref([])
  
  // 获取讲师列表
  async function fetchTeacherList() {
    try {
      // 从localStorage获取数据，如果没有则使用初始数据
      const stored = localStorage.getItem('teacherList')
      if (stored) {
        teacherList.value = JSON.parse(stored)
      } else {
        teacherList.value = [...initialTeachers]
        saveToLocalStorage()
      }
    } catch (error) {
      ElMessage.error('获取讲师列表失败')
    }
  }

  // 添加讲师
  function addTeacher(teacher) {
    const newTeacher = {
      ...teacher,
      id: Date.now(),
      courseCount: 0,
      studentCount: 0,
      rating: 5.0
    }
    teacherList.value.unshift(newTeacher)
    // 保存到localStorage
    saveToLocalStorage()
    return newTeacher
  }

  // 更新讲师
  function updateTeacher(teacher) {
    const index = teacherList.value.findIndex(item => item.id === teacher.id)
    if (index > -1) {
      teacherList.value[index] = { ...teacher }
      // 保存到localStorage
      saveToLocalStorage()
      return true
    }
    return false
  }

  // 删除讲师
  function deleteTeacher(teacherId) {
    const index = teacherList.value.findIndex(item => item.id === teacherId)
    if (index > -1) {
      teacherList.value.splice(index, 1)
      // 保存到localStorage
      saveToLocalStorage()
      return true
    }
    return false
  }

  // 保存到localStorage
  function saveToLocalStorage() {
    localStorage.setItem('teacherList', JSON.stringify(teacherList.value))
  }

  // 从localStorage加载
  function loadFromLocalStorage() {
    const stored = localStorage.getItem('teacherList')
    if (stored) {
      teacherList.value = JSON.parse(stored)
    } else {
      teacherList.value = [...initialTeachers]
      saveToLocalStorage()
    }
  }

  // 初始化时从localStorage加载
  loadFromLocalStorage()

  return {
    teacherList,
    fetchTeacherList,
    addTeacher,
    updateTeacher,
    deleteTeacher
  }
}

// 提供讲师状态
export function provideTeacherStore() {
  const store = createTeacherStore()
  provide(teacherStoreKey, store)
  return store
}

// 注入讲师状态
export function useTeacherStore() {
  const store = inject(teacherStoreKey)
  if (!store) {
    throw new Error('Teacher store not provided')
  }
  return store
} 